﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;

namespace DataCombine
{
    public partial class Form_SheetSelect : Form
    {
         DataCombineMainForm mainForm;

        public List<string> originSheetNames { get; set; }
        public List<string> selectedSheetNames { get; set; }

        public List<string> selectedSheetYearMonth { get; set; }

        public Form_SheetSelect()
        {
            InitializeComponent();
            
        }

        public Form_SheetSelect(List<string> sheetNames)
        {
            originSheetNames = sheetNames;
            InitializeComponent();
        }

        private void button_end_ok_Click(object sender, EventArgs e)
        {
            // 检查年月列, 如果选中的有重复, 不退出
            Console.WriteLine("button_end_ok_Click");
            selectedSheetNames = new List<string>();

            foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                if (dgvr.Cells[2] is DataGridViewCheckBoxCell)
                {
                    if ((bool)((DataGridViewCheckBoxCell)dgvr.Cells[2]).FormattedValue == true)
                    {
                        Console.WriteLine(dgvr.Cells[0].Value.ToString());
                        selectedSheetNames.Add(dgvr.Cells[0].Value.ToString());
                    }
                }
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button_end_cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void Form_SheetSelect_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null; //每次打开清空内容

            mainForm = (DataCombineMainForm)this.Owner;

            if(originSheetNames.Count <= 0)
            {
                MessageBox.Show("文件没有任何sheet");
                return;
            }

            for(int i = 0; i < originSheetNames.Count; i++)
            {
                string sn = originSheetNames[i];
                //Console.WriteLine(sn);
                int index = this.dataGridView1.Rows.Add();
                this.dataGridView1.Rows[index].Cells[0].Value = sn;

                DateTime dt = DateTime.Today;
                string yyyyMM = dt.ToString("yyyy-MM");
                this.dataGridView1.Rows[index].Cells[1].Value = yyyyMM;
                this.dataGridView1.Rows[index].Cells[2].Value = false;
            }
        }

        private void Form_SheetSelect_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void button_select_all_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < dataGridView1.Rows.Count -1; i++)
            {
                DataGridViewRow dgvr = dataGridView1.Rows[i];
                if(dgvr != null)
                {
                    dgvr.Cells[2].Value = true;
                }
            }
        }

        private void button_disselect_all_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                DataGridViewRow dgvr = dataGridView1.Rows[i];
                if (dgvr != null)
                {
                    dgvr.Cells[2].Value = false;
                }
            }
        }

        private void button_guess_year_month_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                if(dgvr.IsNewRow)
                {
                    continue;
                }
                string sheetName = dgvr.Cells[0].Value.ToString();
               // Console.WriteLine(sheetName+ " === " +Util.GuessMonth(sheetName).ToString());
                 
            }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if(e.Button == MouseButtons.Right)
            {
                string s = string.Format("m-r click R{0} C{1}", e.RowIndex, e.ColumnIndex);
                Console.WriteLine(s);
            }
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string s = string.Format("m-2c click R{0} C{1}", e.RowIndex, e.ColumnIndex);
            Console.WriteLine(s);

            if(e.ColumnIndex == 1)
            {
                string cellContent = (string)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
                DateTime dt = DateTime.ParseExact(cellContent, "yyyy-MM", CultureInfo.CurrentCulture, DateTimeStyles.None);

                Form_SelectYearMonth fsym = new Form_SelectYearMonth(dt);
                fsym.Owner = this;
                fsym.ShowDialog();

                dt = fsym.dateTime;
                Console.WriteLine(dt);

                cellContent = dt.ToString("yyyy-MM");
                dataGridView1[e.ColumnIndex, e.RowIndex].Value = cellContent;

                dataGridView1[e.ColumnIndex, e.RowIndex].Selected = false;
                dataGridView1[e.ColumnIndex, e.RowIndex].Selected = true;
                //dataGridView1[e.ColumnIndex, e.RowIndex].
                dataGridView1.EndEdit();
            }

            
        }
    }
}
